package com.sky.service.impl;


import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {


    @Autowired
    private AddressBookMapper addressBookMapper;

    // 保存地址
    @Override
    public void saveAddress(AddressBook addressBook) {
        //每一个地址都要有用户唯一标识
        //设置用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        //设置默认为0
        addressBook.setIsDefault(StatusConstant.DISABLE);
        //插入
        addressBookMapper.insert(addressBook);
    }

    // 查询所有地址
    @Override
    public List<AddressBook> queryAllAddress() {
        //通过用户id查询
        //未来考虑sql语句的复用性  可以进行封装  后面会有查询默认地址
        AddressBook addressBook =AddressBook.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        List<AddressBook> list = addressBookMapper.selectAll(addressBook);
        return list;
    }

    //通过主键id查询
    @Override
    public AddressBook queryById(Long id) {
        AddressBook addressBook =AddressBook.builder()
                .userId(BaseContext.getCurrentId())
                .id(id)
                .build();
        List<AddressBook> list = addressBookMapper.selectAll(addressBook);
        //获取结果,只会存在一条
        return list.get(0);
    }

    //通过主键id和用户id删除
    @Override
    public void delete(Long id) {
        //为了提高安全性,只允许删除当前登录用户的地址
        AddressBook addressBook = AddressBook.builder()
                .userId(BaseContext.getCurrentId())
                .id(id)
                .build();
        addressBookMapper.delete(addressBook);
    }

    //设置默认地址
    @Override
    public void setDefault(AddressBook addressBook) {
        //先将当前用户的所有地址设置非默认
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(StatusConstant.DISABLE);  //0
        //记录当前要设置的默认地址的id
        Long addressBookId = addressBook.getId();
        addressBook.setId(null);
        addressBookMapper.update(addressBook);  //此刻修改多条地址  因为id为 null

        //设置默认
        addressBook.setId(addressBookId);
        addressBook.setIsDefault(StatusConstant.ENABLE); //1
        //提高sql安全性,只允许修改当前登录用户的地址
        addressBookMapper.update(addressBook);  //修改一条地址 因为id为当前id
    }

    //查询默认地址
    @Override
    public AddressBook getDefault() {
        AddressBook addressBook =AddressBook.builder()
                .userId(BaseContext.getCurrentId())
                .isDefault(StatusConstant.ENABLE)
                .build();

        List<AddressBook> list = addressBookMapper.selectAll(addressBook);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    // 修改地址
    @Override
    public void update(AddressBook addressBook) {
        // 设置用户ID
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookMapper.update(addressBook);
    }

    //


}
